package com.goldplusgold.tradedelay.app.market.widget.chartutil;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.Shader;
import android.util.Log;

import com.goldplusgold.network.lib.http.protobuf.Market;
import com.goldplusgold.tradedelay.app.market.pojo.CMinute;
import com.goldplusgold.tradedelay.app.market.pojo.StickData;
import com.goldplusgold.tradedelay.app.market.widget.chartview.KLineView;

import java.util.ArrayList;
import java.util.List;

import static com.goldplusgold.tradedelay.app.market.widget.chartutil.NumberUtil.getAgMoneyString;
import static com.goldplusgold.tradedelay.app.market.widget.chartutil.NumberUtil.getMoneyString;


/**
 * Created by Administrator on 2016/8/5.
 */
public class DrawUtils {
    //分时图时间、价格、涨幅文字大小
    public static final float FENSHI_TEXT_SIZE = 26f;
    //时间文字大小
    public static final float FENSHI_TIME_SIZE = 20f;
    //当文字在最上方的时候，需要往下偏移，如高的涨幅
    public static final float FENSHI_TEXT_OFFSET = 32f;

    private static float lineSmoothness = 0.2f;

    static Paint base_paint = new Paint();
    static Paint blue_paint = new Paint();
    static Paint line_paint = new Paint();
    static Paint candle_paint = new Paint();
    static Paint lineYPrice_paint = new Paint();
    static Paint lineXTime_paint = new Paint();
    static Paint vol_paint = new Paint();
    static Paint volRects_paint = new Paint();
    static Paint macdRects_paint = new Paint();
    static Paint indexMiddleText_paint = new Paint();
    static Paint paint = new Paint();

    static {
        base_paint.setAntiAlias(true);
        base_paint.setStrokeWidth(2.0f);
        blue_paint.setAntiAlias(true);
        blue_paint.setColor(Color.BLUE);
        blue_paint.setStyle(Paint.Style.FILL);

        line_paint.setAntiAlias(true);
        line_paint.setStrokeWidth(2.0f);
        line_paint.setStyle(Paint.Style.STROKE);
        line_paint.setStrokeJoin(Paint.Join.ROUND);

        candle_paint.setAntiAlias(true);
        candle_paint.setStrokeWidth(2.0f);

        lineYPrice_paint.setColor(ColorUtil.COLOR_PING_ASH);
        lineYPrice_paint.setTextSize(FENSHI_TEXT_SIZE);
        lineYPrice_paint.setAntiAlias(true);
        lineYPrice_paint.setTextAlign(Paint.Align.LEFT);

        lineXTime_paint.setTextSize(FENSHI_TIME_SIZE);
        lineXTime_paint.setAntiAlias(true);
        lineXTime_paint.setColor(ColorUtil.COLOR_PING_ASH);

        vol_paint.setAntiAlias(true);
        vol_paint.setTextSize(FENSHI_TEXT_SIZE);
        vol_paint.setColor(ColorUtil.COLOR_PING_ASH);

        volRects_paint.setAntiAlias(true);
        volRects_paint.setTextSize(FENSHI_TEXT_SIZE);
        volRects_paint.setColor(Color.BLACK);
        volRects_paint.setStrokeWidth(1f);

        macdRects_paint.setAntiAlias(true);
        macdRects_paint.setColor(Color.BLACK);

        indexMiddleText_paint.setAntiAlias(true);
        indexMiddleText_paint.setColor(Color.BLACK);
        indexMiddleText_paint.setTextSize(FENSHI_TEXT_SIZE);
    }

    /**
     * 传入价格和
     *
     * @param prices   价格
     * @param xUnit    x轴每两点距离
     * @param height   控件高度
     * @param max      价格最大值
     * @param min      价格最小值
     * @param fromZero 是否从0开始，意思是，假如y轴为0是否画出该点（比如在SMA5、SMA10中，并不是由0开始）
     * @return
     */
    public static float[] getLines(float[] prices, float xUnit, float height, float max, float min, boolean fromZero, float y, float xOffset) {
        float[] result = new float[prices.length * 4];
        float yUnit = (max - min) / height;
        for (int i = 0; i < prices.length - 1; i++) {
            //排除起点为0的点
            if (!fromZero && prices[i] == 0) continue;
            result[i * 4 + 0] = xOffset + i * xUnit;
            result[i * 4 + 1] = y + height - (prices[i] - min) / yUnit;
            result[i * 4 + 2] = xOffset + (i + 1) * xUnit;
            result[i * 4 + 3] = y + height - (prices[i + 1] - min) / yUnit;
        }
        return result;
    }

    public static boolean invalidY(float currentY, float maxY, float minY) {
        if (currentY > maxY || currentY < minY) {
            return true;
        }
        return false;
    }

    /**
     * 传入价格和
     *
     * @param prices   价格
     * @param xUnit    x轴每两点距离
     * @param height   控件高度
     * @param max      价格最大值
     * @param min      价格最小值
     * @param fromZero 是否从0开始，意思是，假如y轴为0是否画出该点（比如在SMA5、SMA10中，并不是由0开始）
     * @return
     */
    public static Path getLinePaths(float[] prices, float xUnit, float height, float max, float min, boolean fromZero, float y, float xOffset, float maxYValue, float minYValue) {
        //目前平均线数据计算方法有问题
        List<LinePoint> mPointList = computerPoints(prices, xUnit, height, max, min, fromZero, y, xOffset, maxYValue, minYValue);
        //三次贝塞尔曲线
        Path path = drawCubicBezier(mPointList);
        return path;
    }


    public static List<LinePoint> computerPoints(float[] prices, float xUnit, float height, float max, float min, boolean fromZero, float y, float xOffset, float maxYValue, float minYValue) {
        List<LinePoint> mPointList = new ArrayList<>();
        float yUnit = (max - min) / height;
        int length = prices.length;
        LinePoint pointt;
        for (int i = 0; i < length - 1; i++) {
            //排除起点为0的点
            if (!fromZero && prices[i] == 0)
                continue;
            pointt = new LinePoint();
            pointt.x = xOffset + i * xUnit;
            pointt.y = y + height - (prices[i] - min) / yUnit;
            if (!invalidY(pointt.y, maxYValue, minYValue)) {
                mPointList.add(pointt);
            }
        }
        return mPointList;
    }

    public static float computeX() {
        return 0f;
    }

    public static float computeY() {
        return 0f;
    }

    protected static Path drawCubicBezier(List<LinePoint> mPointList) {
        Path path = new Path();
        int entryCount = mPointList.size();
        if (entryCount == 0) {
            return path;
        }
        LinePoint entryFrom = mPointList.get(0);
        LinePoint entryTo = mPointList.get(entryCount - 1);
        int diff = (entryFrom == entryTo) ? 1 : 0;
        int minx = Math.max(mPointList.indexOf(entryFrom) - diff, 0);
        int maxx = Math.min(Math.max(minx + 2, mPointList.indexOf(entryTo) + 1), entryCount);
        float phaseX = 1.0f;
        float phaseY = 1.0f;

        int size = (int) Math.ceil((maxx - minx) * phaseX + minx);
        if (size - minx >= 2) {

            float prevDx = 0f;
            float prevDy = 0f;
            float curDx = 0f;
            float curDy = 0f;

            LinePoint prevPrev = mPointList.get(minx);
            LinePoint prev = prevPrev;
            LinePoint cur = prev;
            LinePoint next = mPointList.get(minx + 1);
            // let the spline start
            path.moveTo(cur.x, cur.y * phaseY);
            for (int j = 1, count = Math.min(size, entryCount); j < count; j++) {

                prevPrev = mPointList.get(j == 1 ? 0 : j - 2);
                prev = mPointList.get(j - 1);
                cur = mPointList.get(j);
                next = entryCount > j + 1 ? mPointList.get(j + 1) : cur;
                prevDx = (cur.x - prevPrev.x) * lineSmoothness;
                prevDy = (cur.y - prevPrev.y) * lineSmoothness;
                curDx = (next.x - prev.x) * lineSmoothness;
                curDy = (next.y - prev.y) * lineSmoothness;

                path.cubicTo(prev.x + prevDx, (prev.y + prevDy) * phaseY,
                        cur.x - curDx,
                        (cur.y - curDy) * phaseY, cur.x, cur.y * phaseY);

            }
        } else{
            LinePoint cur = mPointList.get(minx);
            LinePoint next = mPointList.get(minx + 1);
            path.moveTo(cur.x, cur.y * phaseY);
            path.lineTo(next.x, next.y * phaseY);
        }
        return path;
    }

    /**
     * 画线，并且线整体在X轴有偏移
     * 使用：画K线均线时，X轴需要加一点
     */
    public static void drawLineWithXOffset(Canvas canvas, float[] prices, float xUnit, float height, int color, float max, float min, float xOffset) {
        drawLines(canvas, prices, xUnit, height, color, max, min, false, 0, xOffset);
    }

    /**
     * 画线，并且线整体在X轴有偏移
     * 使用：画K线均线时，X轴需要加一点
     */
    public static void drawPathWithXOffset(Canvas canvas, float[] prices, float xUnit, float height, int color, float max, float min, float xOffset, float maxYValue, float minYValue) {
        drawLinePaths(canvas, prices, xUnit, height, color, max, min, false, 0, xOffset, maxYValue, minYValue);
    }

    /**
     * 画线
     *
     * @param canvas
     * @param prices   价格
     * @param xUnit    x轴每两点距离
     * @param height   控件高度
     * @param color    颜色
     * @param max      价格最大值
     * @param min      价格最小值
     * @param fromZero 是否从0开始，意思是，假如y轴为0是否画出该点（比如在SMA5、SMA10中，并不是由0开始）
     */
    public static void drawLines(Canvas canvas, float[] prices, float xUnit, float height, int color, float max, float min, boolean fromZero) {
        drawLines(canvas, prices, xUnit, height, color, max, min, fromZero, 0, 0);
    }


    public static void drawPriceShader(Canvas canvas, float[] prices, float xUnit, float height, float max, float min) {
        float[] points = getLines(prices, xUnit, height, max, min, false, 0, 0);
        if (points == null || points.length == 0) return;
        Shader mShasder = new LinearGradient(0, 0, 0, height, new int[]{Color.parseColor("#aa3483e9"), Color.parseColor("#303483e9")}, null, Shader.TileMode.CLAMP);
        blue_paint.setShader(mShasder);
        for (int i = 1; i < points.length / 4; i++) {
            float x1 = points[i * 4];
            float y1 = points[i * 4 + 1];
            float x2 = points[i * 4 + 2];
            float y2 = points[i * 4 + 3];
            Path path = new Path();
            path.moveTo(x1, height);
            path.lineTo(x1, y1);
            path.lineTo(x2, y2);
            path.lineTo(x2, height);
            path.close();
            canvas.drawPath(path, blue_paint);
        }

    }

    /**
     * 画线
     *
     * @param canvas
     * @param prices   价格
     * @param xUnit    x轴每两点距离
     * @param height   控件高度
     * @param color    颜色
     * @param max      价格最大值
     * @param min      价格最小值
     * @param fromZero 是否从0开始，意思是，假如y轴为0是否画出该点（比如在SMA5、SMA10中，并不是由0开始）
     * @param y        左上角Y轴,指标并不是从左上角开始画
     */
    public static void drawLines(Canvas canvas, float[] prices, float xUnit, float height, int color, float max, float min, boolean fromZero, float y, float xOffset) {
        if (canvas == null) {
            Log.w("DrawUtils", "canvas为空");
            return;
        }
        float tmax = 0f;
        float tmin = 0f;
        for (float f : prices) {
            tmax = tmax > f ? tmax : f;
            tmin = tmin < f ? tmin : f;
        }
        //如果数组中值全为0，则不画该线
        if (tmax == 0 && tmin == 0)
            return;
        line_paint.setColor(color);
        canvas.drawLines(getLines(prices, xUnit, height, max, min, fromZero, y, xOffset), line_paint);
    }

    /**
     * 画线
     *
     * @param canvas
     * @param prices   价格
     * @param xUnit    x轴每两点距离
     * @param height   控件高度
     * @param color    颜色
     * @param max      价格最大值
     * @param min      价格最小值
     * @param fromZero 是否从0开始，意思是，假如y轴为0是否画出该点（比如在SMA5、SMA10中，并不是由0开始）
     * @param y        左上角Y轴,指标并不是从左上角开始画
     */
    public static void drawLinePaths(Canvas canvas, float[] prices, float xUnit, float height, int color, float max, float min, boolean fromZero, float y, float xOffset, float maxYValue, float minYValue) {
        if (canvas == null) {
            Log.w("DrawUtils", "canvas为空");
            return;
        }

        float tmax = 0f;
        float tmin = 0f;
        for (float f : prices) {
            tmax = tmax > f ? tmax : f;
            tmin = tmin < f ? tmin : f;
        }
        //如果数组中值全为0，则不画该线
        if (tmax == 0 && tmin == 0)
            return;

        line_paint.setColor(color);
        float drawScale = 1f;
        Path path = getLinePaths(prices, xUnit, height, max, min, fromZero, y, xOffset, maxYValue, minYValue);
        canvas.drawPath(path, line_paint);
    }

    /**
     * 画烛形图
     *
     * @param canvas
     * @param maxY   最高 传入计算完成的值（对应x,y轴）max/yUnit
     * @param minY   最低 传入计算完成的值（对应x,y轴）
     * @param openY  开盘 传入计算完成的值（对应x,y轴）
     * @param closeY 收盘 传入计算完成的值（对应x,y轴）
     * @param x      x轴 烛形图左上x坐标
     * @param y      y轴坐标 烛形图左上y坐标
     * @param width  屏幕宽度
     */

    public static void drawCandle(Canvas canvas, double yMax, double yMin, float maxY, float minY, float openY, float closeY, float x, float y, float drawCount, float width) {
        if (canvas == null) {
            Log.w("DrawUtils", "canvas为空");
            return;
        }
        //当在坐标系之外，不画
        if (x < 0) return;
        float xUnit = width / drawCount;
        float diff = xUnit - xUnit / KLineView.WIDTH_SCALE;
        //是否上涨,由于计算成了Y轴坐标，所以上面的小，下面的大
        boolean isRise = closeY <= openY;
        candle_paint.setColor(ColorUtil.getTextColorAsh(isRise ? 1 : -1, 0));
        float x_line_start = x + xUnit / 2;
        float x_line_stop = x + xUnit / 2;
        float y_line_start = y;
        float y_line_stop = minY - 1;
        canvas.drawLine(x_line_start, y_line_start, x_line_start, y_line_stop, candle_paint);
        canvas.drawRect(x + diff / 2, y + ((!isRise ? closeY : openY) - maxY), x + xUnit - diff / 2, y + ((!isRise ? openY : closeY) - maxY) + 1, candle_paint);
    }


    public static void drawCandleMaxAndMin(Canvas canvas, Market.InstType instType, double price, float x, float y, float drawCount, float width, boolean isMax) {
        if (canvas == null) {
            Log.w("DrawUtils", "canvas为空");
            return;
        }
        float xUnit = width / drawCount;
        float dy = 40;
        float dx = 120;
        float xWidth = 20;
        float x_line_start = 0f;
        float x_line_stop = 0f;
        float y_line_start = y;
        float rect_x_start = 0f;
        float rect_x_stop = 0f;
        if (isMax) {
            y_line_start = y - 20;
        } else {
            y_line_start = y - dy + 20;
        }
        float right_x = x + xWidth + dx;
        if (right_x > width) {
            x_line_stop = x + xUnit / 2;
            x_line_start = x_line_stop - xWidth;
            rect_x_start = x_line_stop - xWidth - dx;
            rect_x_stop = x_line_stop - xWidth;
        } else {
            x_line_start = x + xUnit / 2;
            x_line_stop = x_line_start + xWidth;
            rect_x_start = x_line_start + xWidth;
            rect_x_stop = x_line_start + xWidth + dx;
        }
        float top2 = y_line_start;
        paint.setColor(Color.BLACK);
        canvas.drawRect(rect_x_start, top2, rect_x_stop, top2 + dy, paint);
        Paint paintt = new Paint();
        paintt.setColor(Color.WHITE);
        paintt.setTextSize(25f);
        paintt.setAntiAlias(true);
        String text = null;
        if (instType.equals(Market.InstType.AG_TD)) {
            text = getAgMoneyString(price);
        } else {
            text = getMoneyString(price);
        }
        float txt_width = getTextWidth(paintt, text);
        float text_start = rect_x_start + (dx - txt_width) / 2;
        canvas.drawText(text, text_start, top2 + 30, paintt);
        paint.setColor(Color.BLACK);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(2.0f);
        canvas.drawLine(x_line_start, top2 + dy / 2, x_line_stop, top2 + dy / 2, paint);
    }


    /**
     * K线：Y轴价格
     *
     * @param canvas
     * @param max
     * @param min
     * @param height
     */
    public static void drawKLineYPrice(Market.InstType instType, Canvas canvas, double max, double min, float height, float textSize) {
        double diff = max - min;
        String p1 = null;
        String p2 = null;
        String p3 = null;
        String p4 = null;
        String p5 = null;
        if (instType == Market.InstType.AG_TD) {
            p1 = getAgMoneyString(max);
            p2 = getAgMoneyString(min + diff * 3 / 4);
            p3 = getAgMoneyString(min + diff * 2 / 4);
            p4 = getAgMoneyString(min + diff * 1 / 4);
            p5 = getAgMoneyString(min);
        } else {
            p1 = getMoneyString(max);
            p2 = getMoneyString(min + diff * 3 / 4);
            p3 = getMoneyString(min + diff * 2 / 4);
            p4 = getMoneyString(min + diff * 1 / 4);
            p5 = getMoneyString(min);
        }
        lineYPrice_paint.setTextSize(textSize);
        canvas.drawText(p1, 0, getTextHeight(p1, lineYPrice_paint), lineYPrice_paint);
        canvas.drawText(p2, 0, height * 1 / 4, lineYPrice_paint);
        canvas.drawText(p3, 0, height * 2 / 4, lineYPrice_paint);
        canvas.drawText(p4, 0, height * 3 / 4, lineYPrice_paint);
        canvas.drawText(p5, 0, height, lineYPrice_paint);
    }

    /**
     * 分时线：Y轴涨跌幅
     *
     * @param canvas
     * @param max    最大值
     * @param min    最小值
     * @param yd     昨收
     */
    public static void drawYPercentAndPrice(Market.InstType instType, Canvas canvas, double max, double min, double yd, float width, float height, float textSize) {
        //计算出最大涨跌幅
        double upPercent = (max - yd) / yd;
        double downPercent = (min - yd) / yd;
        double maxPercent = Math.abs(upPercent) > Math.abs(downPercent) ? upPercent : downPercent;
        double halfPercent = maxPercent / 2d;
        //计算出最大价格
        double diff = Math.abs(max - yd) > Math.abs(min - yd) ? Math.abs(max - yd) : Math.abs(min - yd);
        String p1 = null;
        String p2 = null;
        String p3 = null;
        String p4 = null;
        String lastSettle = null;
        if (instType == Market.InstType.AG_TD) {
            p1 = getAgMoneyString(yd + diff);
            p2 = getAgMoneyString(yd + diff / 2);
            p3 = getAgMoneyString(yd - diff / 2);
            p4 = getAgMoneyString(yd - diff);
            lastSettle = getAgMoneyString(yd);
        } else {
            p1 = getMoneyString(yd + diff);
            p2 = getMoneyString(yd + diff / 2);
            p3 = getMoneyString(yd - diff / 2);
            p4 = getMoneyString(yd - diff);
            lastSettle = getMoneyString(yd);
        }
        Paint p = new Paint();
        p.setColor(ColorUtil.getTextColorAsh(1, 0));
        p.setTextSize(textSize);
        p.setAntiAlias(true);
        p.setTextAlign(Paint.Align.RIGHT);
        //最大涨幅(价格)
        canvas.drawText(NumberUtil.getPercentString(Math.abs(maxPercent)), width, getTextHeight(NumberUtil.getPercentString(Math.abs(maxPercent)), p), p);
        p.setTextAlign(Paint.Align.LEFT);
        canvas.drawText(p1, 0, getTextHeight(p1, p), p);
        //一半涨幅(价格)
        p.setTextAlign(Paint.Align.RIGHT);
        canvas.drawText(NumberUtil.getPercentString(Math.abs(halfPercent)), width, height * 1 / 4, p);
        p.setTextAlign(Paint.Align.LEFT);
        canvas.drawText(p2, 0, height * 1 / 4, p);
        //中间0%
        p.setColor(ColorUtil.getTextColorAsh(0, 0));
        p.setTextAlign(Paint.Align.RIGHT);
        canvas.drawText("0.00%", width, height / 2, p);
        p.setTextAlign(Paint.Align.LEFT);
        canvas.drawText(lastSettle, 0, height * 2 / 4, p);
        //跌幅一半
        p.setColor(ColorUtil.getTextColorAsh(0, 1));
        p.setTextAlign(Paint.Align.RIGHT);
        canvas.drawText("-" + NumberUtil.getPercentString(Math.abs(halfPercent)), width, height * 3 / 4, p);
        p.setTextAlign(Paint.Align.LEFT);
        canvas.drawText(p3, 0, height * 3 / 4, p);
        //最下面跌幅
        p.setTextAlign(Paint.Align.RIGHT);
        canvas.drawText("-" + NumberUtil.getPercentString(Math.abs(maxPercent)), width, height - 4, p);
        p.setTextAlign(Paint.Align.LEFT);
        canvas.drawText(p4, 0, height - 4, p);
        p.reset();
    }

    /**
     * 分时图：X轴时间
     *
     * @param canvas
     * @param height 高度
     * @param width  宽
     */
    public static void drawXTime(Canvas canvas, float width, float height, float textSize) {

        //上面偏移一点
        height = height + 25;
        Paint p = new Paint();
        p.setTextSize(textSize);
        p.setAntiAlias(true);
        p.setColor(ColorUtil.COLOR_PING_ASH);
        p.setTextAlign(Paint.Align.LEFT);
        //左侧日期
        canvas.drawText("20:00", 0, height, p);

        String text = "2:30/9:00";
        float txt_width = getTextWidth(p, text);
        float text_start = txt_width / 2;
        float x_start = 390f / 660f * width;
        canvas.drawText(text, x_start - text_start, height, p);

        text = "11:30/13:30";
        txt_width = getTextWidth(p, text);
        text_start = txt_width / 2;
        x_start = 540f / 660f * width;
        canvas.drawText(text, x_start - text_start, height, p);

        text = "15:30";
        txt_width = getTextWidth(p, text);
        text_start = txt_width;
        x_start = width;
        canvas.drawText(text, x_start - text_start, height, p);
        p.reset();
    }

    /**
     * K线:下面的时间
     *
     * @param canvas
     * @param s1
     * @param s2
     * @param width
     * @param height
     */
    public static void drawKLineXTime(Canvas canvas, String s1, String s2, String s3, float width, float height, float textSize) {
        //上面偏移一点
        //height = height + 28;
        lineXTime_paint.setTextSize(textSize);
        lineXTime_paint.setTextAlign(Paint.Align.LEFT);
        canvas.drawText(s1, 0, height + getTextHeight(s1, lineXTime_paint), lineXTime_paint);
        lineXTime_paint.setTextAlign(Paint.Align.RIGHT);
        if (s2 != null) {
            canvas.drawText(s2, width / 2 + getTextWidth(lineXTime_paint, s2) / 2, height + getTextHeight(s2, lineXTime_paint), lineXTime_paint);
        }
        if (s3 != null) {
            canvas.drawText(s3, width, height + getTextHeight(s3, lineXTime_paint), lineXTime_paint);
        }
    }

    /**
     * 指标1：VOL
     * 画VOL线
     *
     * @param canvas
     * @param xUint   x轴单位距离
     * @param y       左上y坐标
     * @param height  绘画区域高度
     * @param max     最大量
     * @param yd      昨收
     * @param minutes 量集合
     */
    public static void drawVOLRects(Canvas canvas, float xUint, float y, float height, long max, float yd, List<CMinute> minutes, float textSize) {
        float yUnit = height / max;
        for (int i = 0; i < minutes.size(); i++) {
            if (minutes.get(i).getCount() != 0) {
                int color = 0;
                if (i == 0) {
                    color = ColorUtil.getTextColorAsh(minutes.get(i).getPrice() > yd ? 1 : -1, 0);
                } else {
                    color = ColorUtil.getTextColorAsh(minutes.get(i).getPrice() > minutes.get(i - 1).getPrice() ? 1 : -1, 0);
                }
                volRects_paint.setColor(color);
                canvas.drawLine(xUint * i, y + (height - minutes.get(i).getCount() * yUnit), xUint * i, y + height, volRects_paint);
            }
        }
        volRects_paint.setTextSize(textSize);
        volRects_paint.setColor(ColorUtil.COLOR_PING_ASH);
        canvas.drawText(max / 2 + "", 0, y + height / 2 + getTextHeight(max / 2 + "", volRects_paint) / 2, volRects_paint);
        canvas.drawText(0 + "", 0, y + height, volRects_paint);
        canvas.drawText(max + "", 0, y + getTextHeight(max + "", volRects_paint), volRects_paint);
    }

    /**
     * 指标1：VOL
     * 画VOL线,K线使用
     *
     * @param canvas
     * @param y      左上y坐标
     * @param height 绘画区域高度
     * @param max    最大量
     * @param datas  量集合
     */
    public static void drawVOLRects(Canvas canvas, float xUnit, float y, float height, long max, List<StickData> datas,int id,int position) {
        float yUnit = height / max;
        float diff = xUnit - xUnit / KLineView.WIDTH_SCALE;
        if (datas.get(id).getCount() != 0) {
            StickData data = datas.get(id);
            int color = ColorUtil.getTextColorAsh(data.getClose() >= data.getOpen() ? 1 : -1, 0);
            base_paint.setColor(color);
            canvas.drawRect(xUnit * position, y + (height - data.getCount() * yUnit), xUnit * (position + 1) - diff, y + height, base_paint);
        }
    }

    /**
     * 指标1：VOL
     * 画VOL的值,K线使用
     */
    public static void drawVOL(Canvas canvas, float y, float height, long max, float textSize) {
        vol_paint.setTextSize(textSize);
        canvas.drawText(max / 2 + "", 0, y + height / 2 + 20, vol_paint);
        //canvas.drawText(max + "", 0, y+20, vol_paint);
    }

    /**
     * 指标MACD中的MACD值
     *
     * @param canvas
     * @param macds
     * @param max
     * @param min
     * @param height
     * @param y
     * @param xUint
     */
    public static void drawMACDRects(Canvas canvas, float[] macds, float max, float min, float height, float y, float xUint) {
        float yUnit = height / (max - min);
        float halfY = y + height / 2f;
        float diff = xUint - xUint / KLineView.WIDTH_SCALE;
        int i = 0;
        for (float f : macds) {
            macdRects_paint.setColor(f > 0 ? ColorUtil.COLOR_RED : ColorUtil.COLOR_GREEN);
            if (f != 0)
                canvas.drawRect(xUint * i + diff / 2, halfY, xUint * (i + 1) - diff / 2, halfY - f * yUnit - 1, macdRects_paint);
            i++;
        }
    }

    public static void drawIndexMiddleText(Canvas canvas, String text, float y) {
        canvas.drawText(text, 0, y, indexMiddleText_paint);
    }

    public static float getY(float mainH, float input, float yMin, float yUnit) {
        return mainH - (input - yMin) / yUnit;
    }

    public static float getTextWidth(Paint p, String string) {
        float width = 0;
        float[] widths = new float[string.length()];
        p.getTextWidths(string, 0, string.length(), widths);
        for (int i = 0; i < string.length(); i++) {
            width += widths[i];
        }
        return width;
    }

    public static int getTextHeight(String text, Paint paint) {
        Rect bounds = new Rect();
        paint.getTextBounds(text, 0, text.length(), bounds);
        int height = bounds.bottom + bounds.height();
        return height;
    }
}
